package com.metrictracker.datasources;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import com.google.visualization.datasource.DataSourceServlet;
import com.google.visualization.datasource.base.TypeMismatchException;
import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.TableCell;
import com.google.visualization.datasource.datatable.TableRow;
import com.google.visualization.datasource.datatable.value.NumberValue;
import com.google.visualization.datasource.datatable.value.TextValue;
import com.google.visualization.datasource.datatable.value.ValueType;
import com.google.visualization.datasource.query.Query;
import com.metrictracker.model.Metric;
import com.metrictracker.model.MetricDao;
import com.metrictracker.model.MetricGoal;
import com.metrictracker.model.MetricGoalDao;
import com.metrictracker.model.MetricValue;
import com.metrictracker.model.MetricValueDao;
import com.metrictracker.util.ExceptionManager;

// This example extends DataSourceServlet
public class SimpleExampleServlet extends DataSourceServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(SimpleExampleServlet.class.getName());
	
	@Override
	public DataTable generateDataTable(Query query, HttpServletRequest request) {
		// Create a data table,
		DataTable data = new DataTable();
		ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
		cd.add(new ColumnDescription("metricDate", ValueType.TEXT, "Date"));
		cd.add(new ColumnDescription("metricValue", ValueType.NUMBER, "Metric Value"));
		cd.add(new ColumnDescription("goalName", ValueType.TEXT, "Goal Name"));
		cd.add(new ColumnDescription("isGoalMet", ValueType.TEXT, "Goal Met?"));
		cd.add(new ColumnDescription("percentToGoal", ValueType.TEXT, "Percent to Goal"));

		data.addColumns(cd);

		MetricDao metricDao = new MetricDao();
		String metricName = null;
		metricName = (String) request.getParameter("metricName");
		metricName = "foo";
		Metric metric = metricDao.getMetric(metricName);
		
		MetricValueDao valueDao = new MetricValueDao();
		MetricGoalDao goalDao = new MetricGoalDao();
		List<MetricGoal> metricGoals = goalDao.listByProperty("metricKey", metric.getKey());
		Iterator<MetricValue> values =  valueDao.listByProperty("metricKey", metric.getKey()).iterator();
		int j=0;
		while (values.hasNext()) {

			j++;
			log.log(Level.WARNING, "value #" + j);
			
			MetricValue value = values.next();
			for (int i=0; i< metricGoals.size(); i++) {
				String isGoalMet = "N";
				MetricGoal goal = metricGoals.get(i);
				if (value.getTimeFrame().after(goal.getEffectivityDate()) &&
						value.getTimeFrame().before(goal.getExpirationDate())) {
					if (goal.getGoalType().equals(MetricGoal.HIGHISBETTER) && goal.getGoal() <= value.getValue()) {
						isGoalMet = "Y";
					}
					else if (goal.getGoalType().equals(MetricGoal.LOWISBETTER) && goal.getGoal() >= value.getValue()) {
						isGoalMet = "Y";
					}
				}
				
				
				try {
					
					log.log(Level.WARNING, "timeframe: " + value.getTimeFrame());
					log.log(Level.WARNING, "value: " + value.getValue());
					log.log(Level.WARNING, "goal name: " + goal.getName());
					log.log(Level.WARNING, "isGoalMet: " + isGoalMet);
					log.log(Level.WARNING, "percent to goal: " + MessageFormat.format("{0,number,#.##%}", value.getValue() / goal.getGoal()));
					
					TableRow row = new TableRow();
					row.addCell(new TableCell(new TextValue(value.getTimeFrame().toString())));
					row.addCell(new TableCell(new NumberValue(value.getValue())));
					row.addCell(new TableCell(new TextValue(goal.getName())));
					row.addCell(new TableCell(new TextValue(isGoalMet)));
					row.addCell(new TableCell(new TextValue(MessageFormat.format("{0,number,#.##%}", value.getValue() / goal.getGoal()))));
					
					data.addRow(row);
					} catch (TypeMismatchException e) {
					ExceptionManager.logException(log, e);
					return null;
				}
				
			}
		}
		return data;
	}


}